package com.yulinlin.web3j.domain;

import lombok.SneakyThrows;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetBalance;

import java.math.BigInteger;

public class ETHToken implements EIP20Interface {

    private Web3j web3 ;

    private ETHSubject subject;

    public ETHToken(Web3j web3, ETHSubject subject) {
        this.web3 = web3;
        this.subject = subject;
    }

    @Override
    public String name() {
        return "比特币";
    }

    @Override
    public String symbol() {
        return "ETH";
    }

    @Override
    public BigInteger totalSupply() {
        return new BigInteger("211000000");
    }

    @SneakyThrows
    @Override
    public BigInteger balanceOf(String address) {
        if(address == null){
            address = subject.getCredentials().getAddress();
        }
        EthGetBalance ethGetBalance = web3
                .ethGetBalance(address, DefaultBlockParameterName.LATEST)
                .sendAsync()
                .get();
        BigInteger wei = ethGetBalance.getBalance();
        return wei;
    }

    @Override
    public boolean transfer(String to, BigInteger value) {
        return false;
    }

    @Override
    public boolean transferFrom(String from, String to, BigInteger value) {
        return false;
    }

    @Override
    public boolean approve(String spender, BigInteger value) {
        return false;
    }

    @Override
    public BigInteger allowance(String owner, String spender) {
        return null;
    }
}
